﻿
#include "ImgSaveFinishViewControler.h"
#include "FirstViewController.h"
#include "DesignViewController.h"
//分享相关
#include "CAShared.h"

extern std::string changeImagePath;
extern std::vector<CAImage*> UserSelectImg;
extern std::string ImageName;

extern CAImage* c_Image;

//IOS平台分享修改图片
extern CAImage* c_ImageName;

ImgSaveFinishViewControler::ImgSaveFinishViewControler()
{

}

ImgSaveFinishViewControler::~ImgSaveFinishViewControler()
{

}

void ImgSaveFinishViewControler::viewDidLoad()
{
	DRect winRect = this->getView()->getBounds();
	DSize winSize = this->getView()->getBounds().size;

	//视图高斯模糊背景
	CAImageView* imageView = CAImageView::createWithImage(UserSelectImg.at(1));
	imageView->setFrame(winRect);
	this->getView()->insertSubview(imageView, 0);

	//渐变背景
	CAImage* shade_image = CAImage::create("source_pro/saveimage/shade.png");
	CAImageView* shade_imageView = CAImageView::createWithImage(shade_image);
	shade_imageView->setFrame(this->getView()->getBounds());
	this->getView()->insertSubview(shade_imageView, 1);

	//对勾资源
	CAImage* _image = CAImage::create("source_pro/saveimage/saveimage.png");
	CAImageView* imageViews = CAImageView::createWithImage(_image);
	imageViews->setImageViewScaleType(CAImageViewScaleTypeFitImageXY);
	imageViews->setCenter(DRect(winSize.width*0.3, winSize.height*0.2, 45, 45));
	this->getView()->insertSubview(imageViews, 2);

	CALabel* fontlable = CALabel::createWithFrame(DRect(winSize.width*0.38, winSize.height*0.18, 250, 50));
	fontlable->setText(UTF8("照片已保存到相册"));
	fontlable->setFontSize(_px(30));
	fontlable->setColor(CAColor_white);
	this->getView()->insertSubview(fontlable, 2);

	//分割线
	CAImage* line_image = CAImage::create("source_pro/saveimage/line.png");
	CAImageView* imageView_line = CAImageView::createWithImage(line_image);
	imageView_line->setFrame(DRect(0, winSize.height*0.3, winSize.width, 6));
	imageView_line->setScale(0.7f);
	this->getView()->insertSubview(imageView_line, 2);

	//分享至
	CALabel* sharelable = CALabel::createWithFrame(DRect(winSize.width*0.15, winSize.height*0.34, 150, 50));
	sharelable->setText(UTF8("分享至"));
	sharelable->setFontSize(_px(28));
	sharelable->setColor(CAColor_white);
	this->getView()->insertSubview(sharelable, 2);

	//导航栏
	CANavigationBarItem* temp_nav = CANavigationBarItem::create(UTF8(""));
	CABarButtonItem* save_leftItem = CABarButtonItem::create(UTF8("< 返回"), NULL, NULL);
	save_leftItem->setItemWidth(110);
	CABarButtonItem* save_rightItem = CABarButtonItem::create(UTF8("首页"), NULL, NULL);
	save_rightItem->setItemWidth(110);

	temp_nav->setShowGoBackButton(false);
	temp_nav->addLeftButtonItem(save_leftItem);
	temp_nav->addRightButtonItem(save_rightItem);
	save_leftItem->setTarget(this, CAControl_selector(ImgSaveFinishViewControler::BackCallBackDown));
	save_rightItem->setTarget(this, CAControl_selector(ImgSaveFinishViewControler::FirstCallBackDown));

	CANavigationBar* tem_nav = CANavigationBar::createWithFrame(DRect(0, 0, winSize.width, 98));
	CAImageView*img = CAImageView::createWithImage(CAImage::create("source_pro/up_navigationbar.png"));
	tem_nav->setBackgroundView(img);
	tem_nav->setItem(temp_nav);
	//this->getView()->addSubview(tem_nav);
	this->getView()->insertSubview(tem_nav, 3);

	/************************************************************************/
	/* 各个平台分享UI                                                       */
	/************************************************************************/

	//分享微信好友button(自定义按钮)
	CAButton* wechat_button = CAButton::create(CAButtonTypeCustom);
	//设置Frame
	wechat_button->setFrame(DRect(winSize.width*0.13, winSize.height*0.42, 116, 116));
	//设置button背景图(支持9宫图)
	wechat_button->setBackGroundViewForState(CAControlStateNormal, CAScale9ImageView::createWithImage
		(CAImage::create("source_pro/saveimage/Wechat.png")));
	//添加到绘制
	this->getView()->insertSubview(wechat_button, 2);
	//文字图片
	CAImageView* wechat_image = CAImageView::createWithImage(CAImage::create("source_pro/saveimage/Wechat_text.png"));
	wechat_image->setFrame(DRect(winSize.width*0.13, winSize.height*0.53, 103, 24));
	this->getView()->insertSubview(wechat_image, 2);


	//分享朋友圈button(自定义按钮)
	CAButton* wechatFriends_button = CAButton::create(CAButtonTypeCustom);
	//设置Frame
	wechatFriends_button->setFrame(DRect(winSize.width*0.43, winSize.height*0.42, 116, 116));
	//设置button背景图(支持9宫图)
	wechatFriends_button->setBackGroundViewForState(CAControlStateNormal, CAScale9ImageView::createWithImage
		(CAImage::create("source_pro/saveimage/WechatFriends.png")));
	//添加到绘制
	this->getView()->insertSubview(wechatFriends_button, 2);
	//文字图片
	CAImageView* wechatFriends_image = CAImageView::createWithImage(CAImage::create("source_pro/saveimage/WechatFriends_text.png"));
	wechatFriends_image->setFrame(DRect(winSize.width*0.455, winSize.height*0.53, 76, 24));
	this->getView()->insertSubview(wechatFriends_image, 2);

	//分享qq好友button(自定义按钮)
	CAButton* qq_button = CAButton::create(CAButtonTypeCustom);
	//设置Frame
	qq_button->setFrame(DRect(winSize.width*0.73, winSize.height*0.42, 116, 116));
	//设置button背景图(支持9宫图)
	qq_button->setBackGroundViewForState(CAControlStateNormal, CAScale9ImageView::createWithImage
		(CAImage::create("source_pro/saveimage/qq.png")));
	//添加到绘制
	this->getView()->insertSubview(qq_button, 2);
	//文字图片
	CAImageView* qq_image = CAImageView::createWithImage(CAImage::create("source_pro/saveimage/qq_text.png"));
	qq_image->setFrame(DRect(winSize.width*0.745, winSize.height*0.53, 92, 24));
	this->getView()->insertSubview(qq_image, 2);

	//分享新浪微博button(自定义按钮)
	CAButton* sinaWeibo_button = CAButton::create(CAButtonTypeCustom);
	//设置Frame
	sinaWeibo_button->setFrame(DRect(winSize.width*0.13, winSize.height*0.6, 116, 116));
	//设置button背景图(支持9宫图)
	sinaWeibo_button->setBackGroundViewForState(CAControlStateNormal, CAScale9ImageView::createWithImage
		(CAImage::create("source_pro/saveimage/SinaWeibo.png")));
	//添加到绘制      
	this->getView()->insertSubview(sinaWeibo_button, 2);
	//文字图片
	CAImageView* sinaWeibo_image = CAImageView::createWithImage(CAImage::create("source_pro/saveimage/SinaWeibo_text.png"));
	sinaWeibo_image->setFrame(DRect(winSize.width*0.13, winSize.height*0.71, 103, 24));
	this->getView()->insertSubview(sinaWeibo_image, 2);

	//分享QQ空间button(自定义按钮)
	CAButton* qqZone_button = CAButton::create(CAButtonTypeCustom);
	//设置Frame
	qqZone_button->setFrame(DRect(winSize.width*0.43, winSize.height*0.6, 116, 116));
	//设置button背景图(支持9宫图)
	qqZone_button->setBackGroundViewForState(CAControlStateNormal, CAScale9ImageView::createWithImage
		(CAImage::create("source_pro/saveimage/qqZone.png")));
	//添加到绘制
	this->getView()->insertSubview(qqZone_button, 2);
	//文字图片
	CAImageView* qqZone_image = CAImageView::createWithImage(CAImage::create("source_pro/saveimage/qqZone_text.png"));
	qqZone_image->setFrame(DRect(winSize.width*0.445, winSize.height*0.71, 90, 25));
	this->getView()->insertSubview(qqZone_image, 2);

	//分享按钮设置监听
	wechat_button->addTarget(this, CAControl_selector(ImgSaveFinishViewControler::wechatCallBackDown), CAControlEventTouchDown);
	wechatFriends_button->addTarget(this, CAControl_selector(ImgSaveFinishViewControler::wechatFriendsCallBackDown), CAControlEventTouchDown);
	qq_button->addTarget(this, CAControl_selector(ImgSaveFinishViewControler::qqCallBackDown), CAControlEventTouchDown);
	sinaWeibo_button->addTarget(this, CAControl_selector(ImgSaveFinishViewControler::sinaWeiboCallBackDown), CAControlEventTouchDown);
	qqZone_button->addTarget(this, CAControl_selector(ImgSaveFinishViewControler::qqZoneCallBackDown), CAControlEventTouchDown);

	//添加资源原图资源
	CAImageView* imageViewss = CAImageView::createWithImage(c_ImageName);
	this->getView()->addSubview(imageViewss);
}

void ImgSaveFinishViewControler::viewDidUnload()
{

}

//分享各个平台button回调函数
void ImgSaveFinishViewControler::wechatCallBackDown(CAControl* control, CCPoint point)
{
	//CCLog(UTF8("微信好友"));
	#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
		android_ShowShared(ImageName, WeChatFriends);
	#endif

	#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
		ios_ShowShared(c_ImageName, WeChatFriends);
	#endif
}
void ImgSaveFinishViewControler::wechatFriendsCallBackDown(CAControl* control, CCPoint point)
{
	//CCLog(UTF8("微信朋友圈"));
	#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
		android_ShowShared(ImageName, Moments);
	#endif

	#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
		ios_ShowShared(c_ImageName, Moments);
	#endif
}
void ImgSaveFinishViewControler::qqCallBackDown(CAControl* control, CCPoint point)
{
	//CCLog(UTF8("qq好友"));
	#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
		android_ShowShared(ImageName, QQFriends);
	#endif

	#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
		ios_ShowShared(c_ImageName, QQFriends);
	#endif
}
void ImgSaveFinishViewControler::sinaWeiboCallBackDown(CAControl* control, CCPoint point)
{
	CAAlertView* alertView = CAAlertView::createWithText(UTF8("提示"), UTF8("抱歉，暂不支持该平台分享！"), UTF8("确定"), NULL);
	alertView->show();
}
void ImgSaveFinishViewControler::qqZoneCallBackDown(CAControl* control, CCPoint point)
{
	#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
		android_ShowShared(ImageName, QQZone);
	#endif

	#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
		ios_ShowShared(c_ImageName, QQZone);
	#endif
}

//返回上一页
void ImgSaveFinishViewControler::BackCallBackDown(CAControl* control, CCPoint point)
{
	//CCLog(UTF8("跳转上一视图"));
	RootWindow::getInstance()->getRootNavigationController()->popViewControllerAnimated(true);
}

//切换到首页
void ImgSaveFinishViewControler::FirstCallBackDown(CAControl* control, CCPoint point)
{
	//CCLog(UTF8("首页按钮"));
	CCSize winRect = this->getView()->getBounds().size;

	FirstViewController* firstControler = new FirstViewController();
	firstControler->init();
	//弹出保存图片的viewcontrol
	RootWindow::getInstance()->getRootNavigationController()->pushViewController(firstControler, true);
	firstControler->release();
}